package com.sinaapp.zygl.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinaapp.zygl.bean.Business;
import com.sinaapp.zygl.bean.UserAndCustomer;
import com.sinaapp.zygl.dao.BusinessDao;
import com.sinaapp.zygl.dao.impl.BusinessDaoImpl;

public class BusinessAction extends ActionSupport {

	private static final long serialVersionUID = -4430931290971596106L;

	@Override
	public String execute() throws Exception {
		BusinessDao dao = new BusinessDaoImpl();
		List<Business> bpagelist = new ArrayList<Business>();
		Business b = new Business();
		b.setB_id(b_id);
		b.setUid(uid);
		b.setCid(cid);
		b.setB_business(b_business);
		b.setB_evaluate(b_evaluate);
		bpagelist = dao.getBusinesss(page, rows, sort, order, b);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", dao.queryBusinessTotal(b));
		m.put("rows", bpagelist);
		writeJson(m);
		return null;
	}

	public String show_business_relation() throws Exception {
		BusinessDao dao = new BusinessDaoImpl();
		List<UserAndCustomer> vpagelist = new ArrayList<UserAndCustomer>();
		UserAndCustomer b = new UserAndCustomer();
		b.setUid(uid);
		b.setName(name);
		b.setCid(cid);
		b.setCname(cname);
		b.setB_business(b_business);
		b.setB_evaluate(b_evaluate);
		vpagelist = dao.getUserAndCustomer(page, rows, sort, order, b);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", dao.queryUserAndCustomerTotal(b));
		m.put("rows", vpagelist);
		writeJson(m);
		return null;
	}

	public String add() throws Exception {
		BusinessDao dao = new BusinessDaoImpl();
		Business b = new Business();
		b.setUid(uid);
		b.setCid(cid);
		b.setB_business(b_business);
		b.setB_evaluate(b_evaluate);
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.add(b)) {
			m.put("business", b);
			m.put("success", true);
		} else {
			m.put("business", b);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public String update() throws Exception {
		BusinessDao dao = new BusinessDaoImpl();
		Business b = new Business();
		b.setB_id(b_id);
		b.setUid(uid);
		b.setCid(cid);
		b.setB_business(b_business);
		b.setB_evaluate(b_evaluate);
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.update(b)) {
			m.put("business", b);
			m.put("success", true);
		} else {
			m.put("business", b);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public String delete() throws Exception {
		BusinessDao dao = new BusinessDaoImpl();
		Map<String, Object> m = new HashMap<String, Object>();
		if (dao.delete(b_id)) {
			m.put("b_id", b_id);
			m.put("success", true);
		} else {
			m.put("b_id", b_id);
			m.put("success", false);
		}
		writeJson(m);
		return null;
	}

	public void writeJson(Object o) {
		// String json = getJsonString(o);
		try {
			// ServletActionContext.getResponse().getWriter().write(json);
			String json = JSON.toJSONStringWithDateFormat(o, "yyyy-MM-dd HH:mm:ss");
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (Exception e) {
		}
	}

	private int page;
	private int rows;
	private String sort;
	private String order;

	private int b_id;
	private String uid;
	private String cid;
	private String b_business;
	private String b_evaluate;

	private String name;
	private String cname;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getB_id() {
		return b_id;
	}

	public void setB_id(int b_id) {
		this.b_id = b_id;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getB_business() {
		return b_business;
	}

	public void setB_business(String b_business) {
		this.b_business = b_business;
	}

	public String getB_evaluate() {
		return b_evaluate;
	}

	public void setB_evaluate(String b_evaluate) {
		this.b_evaluate = b_evaluate;
	}

}
